package br.ita.comp.ces22.quiz.web.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import br.ita.comp.ces22.quiz.domain.User;

public class AdminAction extends Action{
	private DynaActionForm adminForm;
	private HttpSession adminSession;
//	private HttpServletRequest adminRequest;
	private User user;
	
	public AdminAction() {
		new ActionDaoManager();
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {		

		setAdminForm((DynaActionForm) form);
		setAdminSession(request.getSession());
//		setAdminRequest(request);
		setUser(ActionDaoManager.getUserDao().getById(
				(String) adminSession.getAttribute("loggedUser")));

		if (!adminForm.get("loginToAdd").equals("")) { 		  
			return registerObserver(mapping);
		} 
		else if (!adminForm.get("loginToRemove").equals("")){
			return removeObserver(mapping);
		} 
		else return mapping.findForward("adminsuccess");
	}
	
	public ActionForward registerObserver(ActionMapping mapping){
		User updateuser;
		updateuser = ActionDaoManager.getUserDao().getById((String) adminForm.get("loginToAdd")); 
		
		adminSession.setAttribute("loggedUser", user.getLogin());

		if(updateuser == null){
	//		System.out.println("aquiiiiiiiiiiii!!!");
		}
		else{
			updateuser.setObserver(1);
			ActionDaoManager.getUserDao().update(updateuser);
			
			ArrayList observers = (ArrayList) ActionDaoManager.getUserDao().getObservers();
			adminSession.setAttribute("ListObservers", observers);
			
			ArrayList nonobservers = (ArrayList) ActionDaoManager.getUserDao().getNonObservers();
			adminSession.setAttribute("ListNonObservers",nonobservers);
			
		}
		return mapping.findForward("adminsuccess");
	}
	public ActionForward removeObserver(ActionMapping mapping){
		User updateuser;
		updateuser = ActionDaoManager.getUserDao().getById((String) adminForm.get("loginToRemove")); 
		adminSession.setAttribute("loggedUser", user.getLogin());
		
		if(updateuser == null){
			
		}
		else{
			updateuser.setObserver(0);
			ActionDaoManager.getUserDao().update(updateuser);
			
			ArrayList observers = (ArrayList) ActionDaoManager.getUserDao().getObservers();
			adminSession.setAttribute("ListObservers", observers);
			
			ArrayList nonobservers = (ArrayList) ActionDaoManager.getUserDao().getNonObservers();
			adminSession.setAttribute("ListNonObservers",nonobservers);
		}
		return mapping.findForward("adminsuccess");
	}
	
	public void setUser(User user) {
		this.user = user;
	}

	public void setAdminForm(DynaActionForm adminForm) {
		this.adminForm = adminForm;
	}

//	public void setAdminRequest(HttpServletRequest adminRequest) {
//		this.adminRequest = adminRequest;
//	}

	public void setAdminSession(HttpSession adminSession) {
		this.adminSession = adminSession;
	}
}